package com.fxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxm.dao.NoticeMapper;
import com.fxm.domain.Notice;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.service.NoticeService;
import com.fxm.utils.TimeUtilCustomize;
import com.fxm.vo.param.NoticeParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-24
 */
@Slf4j
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public RespEntity<List<Notice>> noticeList() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Notice::getDeleted, 0);
        List<Notice> noticeList = baseMapper.selectList(queryWrapper);

        if (Objects.isNull(noticeList) || noticeList.size() == 0) {
            return new RespEntity(RespCode.SUCCESS);
        }

        noticeList = noticeList.stream()
                .sorted(Comparator.comparing(Notice::getId).reversed())
                .collect(Collectors.toList());

        return new RespEntity(RespCode.SUCCESS, noticeList);
    }

    @Override
    public RespEntity insertNotice(NoticeParam noticeParam) {
        String date = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_MD_ZH, new Date());

        Notice notice = Notice.builder()
                .noticeTime(date)
                .noticeContent(noticeParam.getContent())
                .build();

        baseMapper.insert(notice);
        return new RespEntity(RespCode.SUCCESS);
    }
}
